﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Site;

public partial class Functions_Default : PageBase
{
    protected List<Site_Functions> dt_groupList = null;
    public List<Site_Functions> dt_GroupList
    {
        get { return dt_groupList; }
        set { dt_groupList = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFunctionList();
        }
    }

    protected void BindFunctionList()
    {
        FunctionBLL objF = new FunctionBLL();
        dt_GroupList = objF.GetParentFunctions();

        rpt_GroupList.DataSource = dt_GroupList;
        rpt_GroupList.DataBind();
    }

    protected void rpt_GroupList_ItemCommand(object sender, RepeaterCommandEventArgs E)
    {
        if (E.CommandName == "Add")
        {
            DropDownList ddl_NewG = (DropDownList)E.Item.FindControl("ddl_NewG");
            TextBox txt_NewArea = (TextBox)E.Item.FindControl("txt_NewArea");
            TextBox txt_NewEnterUrl = (TextBox)E.Item.FindControl("txt_NewEnterUrl");
            TextBox txt_NewFunctionName = (TextBox)E.Item.FindControl("txt_NewFunctionName");
            TextBox txt_NewFunctionOrder = (TextBox)E.Item.FindControl("txt_NewFunctionOrder");

            Site_Functions model = new Site_Functions();
            model.Area = txt_NewArea.Text;
            model.EnterUrl = txt_NewEnterUrl.Text;
            model.FunctionName = txt_NewFunctionName.Text;
            model.ParentID = int.Parse(ddl_NewG.SelectedValue);
            model.OrderNum = int.Parse(txt_NewFunctionOrder.Text);

            FunctionBLL objBLL = new FunctionBLL();
            objBLL.Add(model);
            
            BindFunctionList();
        }

        if (E.CommandName == "Delete")
        {
            FunctionBLL objBLL = new FunctionBLL();
            objBLL.Delete(int.Parse(E.CommandArgument.ToString()));            

            BindFunctionList();
        }

        if (E.CommandName == "Edit")
        {
            TextBox txt_GroupName = (TextBox)E.Item.FindControl("txt_GroupName");
            TextBox txt_SectionID = (TextBox)E.Item.FindControl("txt_SectionID");
            TextBox txt_GroupOrder = (TextBox)E.Item.FindControl("txt_GroupOrder");
            TextBox txt_SectionName = (TextBox)E.Item.FindControl("txt_SectionName");

            Site_Functions model = new Site_Functions();
            model.FunctionID = Convert.ToInt32(E.CommandArgument);
            model.SectionID = int.Parse(txt_SectionID.Text);
            model.OrderNum = int.Parse(txt_GroupOrder.Text);
            model.FunctionName = txt_GroupName.Text;
            model.SectionName = txt_SectionName.Text;

            FunctionBLL objBLL = new FunctionBLL();
            objBLL.Update(model);

            BindFunctionList();
        }
    }
    protected void rpt_GroupList_ItemDataBound(object sender, RepeaterItemEventArgs E)
    {
        Literal lit_GroupID = (Literal)E.Item.FindControl("lit_GroupID");
        Repeater rpt_FunctionList = (Repeater)E.Item.FindControl("rpt_FunctionList");
        DropDownList ddl_NewG = (DropDownList)E.Item.FindControl("ddl_NewG");
        ddl_NewG.DataSource = dt_GroupList;
        ddl_NewG.DataTextField = "FunctionName";
        ddl_NewG.DataValueField = "FunctionID";
        ddl_NewG.DataBind();
        ddl_NewG.SelectedValue = lit_GroupID.Text;

        FunctionBLL objBLL = new FunctionBLL();
        
        rpt_FunctionList.DataSource = objBLL.GetFunctionsByParentID(int.Parse(lit_GroupID.Text));
        rpt_FunctionList.DataBind();
        rpt_FunctionList.ItemCommand += new RepeaterCommandEventHandler(rpt_FunctionList_ItemCommand);
    }

    protected void rpt_FunctionList_ItemCommand(object sender, RepeaterCommandEventArgs E)
    {
        if (E.CommandName == "Delete")
        {          
            FunctionBLL objBLL = new FunctionBLL();
            objBLL.Delete(int.Parse(E.CommandArgument.ToString())); 
      
            BindFunctionList();
        }

        if (E.CommandName == "Edit")
        {
            TextBox txt_AreaName = (TextBox)E.Item.FindControl("txt_AreaName");
            TextBox txt_EnterUrl = (TextBox)E.Item.FindControl("txt_EnterUrl");
            TextBox txt_OrderNum = (TextBox)E.Item.FindControl("txt_OrderNum");
            TextBox txt_FunctionName = (TextBox)E.Item.FindControl("txt_FunctionName");
            DropDownList ddl_GroupID = (DropDownList)E.Item.FindControl("ddl_GroupID");

            Site_Functions model = new Site_Functions();
            model.Area = txt_AreaName.Text;
            model.EnterUrl = txt_EnterUrl.Text;
            model.FunctionName = txt_FunctionName.Text;
            model.OrderNum = int.Parse(txt_OrderNum.Text);
            model.FunctionID = Convert.ToInt32(E.CommandArgument);
            model.ParentID = int.Parse(ddl_GroupID.SelectedValue);
           
            FunctionBLL objBLL = new FunctionBLL();
            objBLL.Update(model);

            BindFunctionList();
        }
    }
    protected void rpt_FunctionList_ItemDataBound(object sender, RepeaterItemEventArgs E)
    {
        HtmlInputHidden hid_GroupID = (HtmlInputHidden)E.Item.FindControl("hid_GroupID");
        DropDownList ddl_GroupID = (DropDownList)E.Item.FindControl("ddl_GroupID");

        ddl_GroupID.DataSource = dt_GroupList;
        ddl_GroupID.DataTextField = "FunctionName";
        ddl_GroupID.DataValueField = "FunctionID";
        ddl_GroupID.DataBind();
        ddl_GroupID.SelectedValue = hid_GroupID.Value;
    }

    protected void btn_AddF_Click(object sender, EventArgs e)
    {
        Site_Functions model = new Site_Functions();
        model.ParentID = 0;
        model.FunctionName = txt_NewGroupName.Text;
        model.OrderNum = int.Parse(txt_NewGroupOrder.Text);
        model.SectionID = int.Parse(txt_NewSectionID.Text);
        model.SectionName = txt_NewSectionName.Text;

        FunctionBLL objBLL = new FunctionBLL();
        objBLL.Add(model);

        txt_NewGroupName.Text = "";
        txt_NewGroupOrder.Text = "";
        txt_NewSectionID.Text = "";
        txt_NewSectionName.Text = "";

        BindFunctionList();
    }
}
